#include "CppUTestExt/MockSupport.h"
#include "CppUTest/TestHarness.h"
#include "scenario_manager.h"
#include "softbus_adapter_mem.h"
#include "softbus_adapter_thread.h"
#include "softbus_def.h"
#include "softbus_error_code.h"

static struct ScenarioManager *manager;

const char *LOCAL_MAC_1 = "18:65:90:ce:8e:b1";
const char *LOCAL_MAC_2 = "82:13:21:36:04:80";

const int LOCAL_PID_1 = 1024;
const int LOCAL_PID_2 = 1025;
//const int LOCAL_PID_3 = 1026;

const char *PEER_MAC_1 = "de:4f:de:77:72:23";
const char *PEER_MAC_2 = "0a:65:90:ce:8e:b2";

#define MOCK_WIFI_EXPECT_ONECALL(localMac, peerMac, finalType) \
    do { \
        mock("WIFI") \
            .expectOneCall("Utils_NotifyWifi") \
            .withParameter("localMac", localMac) \
            .withParameter("peerMac", peerMac) \
            .withParameter("finalType", finalType); \
    } while(0)
        
TEST_GROUP(scenario_manager_test)
{
    void setup()
    {
	manager = ScenarioManagerGetInstance();
        CHECK_FALSE(manager == NULL);
        //LONGS_EQUAL(2, tmpValue);
        printf("%s: setup here!\n", __func__);
        printf("\n");
        printf("\n");
    }

    void teardown()
    {
        ScenarioManagerdestroyInstance(manager);
        printf("%s: teardown here!\n", __func__);
        printf("\n");
        printf("\n");

        mock("WIFI").checkExpectations();
        mock().clear();
    }
};

TEST(scenario_manager_test, should_add_one_scene_with_video_type)
{
    printf("%s: start ...\n", __func__);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 1);

    int ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    printf("%s: should_add_one_scene_with_video_type end!!! ...\n", __func__);
    printf("\n");
    printf("\n");
}

TEST(scenario_manager_test, should_add_too_scene_with_file_type)
{
    printf("%s: start ...\n", __func__);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 1);

    int ret1 = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret1);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_2, 1);
    int ret2 = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret2);
    printf("%s: should_add_too_scene_with_file_type end!!! ...\n", __func__);
    printf("\n");
    printf("\n");
}

TEST(scenario_manager_test, should_del_scene_after_add)
{
    printf("%s: start ...\n", __func__);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 1);

    int ret1 = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret1);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 0);
    int ret2 = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret2);

    printf("%s: should_del_scene_after_add end!!! ...\n", __func__);
    printf("\n");
    printf("\n");
}

TEST(scenario_manager_test, should_return_err_or_notify_wifi_when_del_none_exist_scene)
{
    printf("%s: start ...\n", __func__);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 1);

    int ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(SOFTBUS_NOT_FIND, ret);

    printf("%s: should_return_err_or_notify_wifi_when_del_none_exist_scene end!!! ...\n", __func__);
    printf("\n");
    printf("\n");
}

TEST(scenario_manager_test, should_not_notfi_when_business_count_more_than_one)
{
    printf("%s: start ...\n", __func__);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 1);
    int ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);

    printf("%s: should_not_notfi_when_business_count_more_then_one end!!! ...\n", __func__);
    printf("\n");
    printf("\n");
}

static void do_multi_cross_add(void)
{
    printf("%s: start ...\n", __func__);
    int ret = -1;

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 1);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 3);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 7);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_2, 1);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_2, 3);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_2, 7);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_1, 1);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_1, 3);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_1, 7);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_2, 1);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_2, 3);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_2, 7);
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerAddScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    printf("%s: do_multi_cross_add end!!! ...\n", __func__);
    printf("\n");
    printf("\n");
}

static void do_multi_cross_del(void)
{
    printf("%s: start ...\n", __func__);
    int ret = -1;

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 6);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 4);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_1, 0);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_1, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_2, 6);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_2, 4);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_1, PEER_MAC_2, 0);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_1, PEER_MAC_2, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_1, 6);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_1, 4);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_1, 0);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_1, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);

    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_1, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_1, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //mock("WIFI").expectNoCall("Utils_NotifyWifi");
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_1, FILE_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_2, 6);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, VIDEO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_2, 4);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, AUDIO_TYPE);
    LONGS_EQUAL(0, ret);
    //MOCK_WIFI_EXPECT_ONECALL(LOCAL_MAC_2, PEER_MAC_2, 0);
    ret = ScenarioManagerDelScenario(manager, LOCAL_MAC_2, PEER_MAC_2, LOCAL_PID_2, FILE_TYPE);
    LONGS_EQUAL(0, ret);
    printf("%s: do_multi_cross_del end!!! ...\n", __func__);
    printf("\n");
    printf("\n");
}

TEST(scenario_manager_test, should_add_multi_scene_when_all_params_cross)
{
    do_multi_cross_add();
    do_multi_cross_del();
}
